package com.muyi.app;

import com.muyi.spring.annotation.Component;
import com.muyi.spring.beans.BeanPostProcessor;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

@Component
public class AopBeanPostProcesser implements BeanPostProcessor {

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) {

        if ("userServiceImpl".equals(beanName)) {

            Object proxyInstance = Proxy.newProxyInstance(AopBeanPostProcesser.class.getClassLoader(), bean.getClass().getInterfaces(), new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    System.out.println("aop before");
                    Object object = method.invoke(bean, args);
                    System.out.println("aop before return");
                    return object;
                }
            });

            return proxyInstance;
        }
        return bean;
    }
}
